"""
@author: yjx
@file: shopping2.py
@time: 2023/4/20 7:43
"""
import json
import random
# 定义用户类
'''
属性： 名字、金豆、
方法：注册、登录
'''

# 定义用户类
'''
属性：房间号、房间人数
方法：创建房间、进入房间
'''


# 加载用户信息
# 将用户信息写入字典{"name":"gold"}
def load_user():
    # 第一次读字典是空信息的时候会报错，读不了，所以我们要用try...except捕捉异常，如果读取文件失败就新建一个为空的用户字典
    try:
        with open("shopping_user.json", "r", encoding="utf-8") as fp:
            user_dict = json.load(fp)
            # print("我们现在的 用户有：", user_dict)
    except:
        user_dict = {}
    return user_dict


# 写入用户信息
def up_user(user_dict):
    with open("shopping_user.json", "w", encoding="utf-8") as fp:
        json.dump(user_dict, fp)


# 加载房间信息
# 将房间信息写入字典{"room_id":"user_num"}
def load_room():
    try:
        with open("shopping_room.json", "r", encoding="utf-8") as fp:
            room_dict = json.load(fp)
    except:
        room_dict = {}
    return room_dict


# 写入房间信息（对于已经存在的房间，进去就可以玩游戏，如果是自己创建的房间还要要邀请一个伙伴跟你一起玩游戏）
def up_room(room_dict):
    with open("shopping_room.json", "w", encoding="utf-8") as fp:
        json.dump(room_dict, fp)


class User:
    # 新建用户
    def __init__(self, name):
        self.name = name
        self.gold = 1000

    # 用户登录
    def login(self, user_dict, name):
        # flag = 0
        # for i in user_dict:
        #     if c1 == i:
        #         flag = 1
        #         break
        if name in user_dict:
            print("恭喜你，登录成功")
        else:
            print("用户还未被注册，或者输入错误！")


class Room:
    # 新建房间
    def __init__(self, room_id):
        self.room_id = room_id
        self.room_user_num = 0

    # 进入房间
    def get_into_room(self, room, room_dict):
        if room.room_id in room_dict:
            # {'123': {'num': 0}}
            # print(room.room_user_num, type(room.room_user_num))
            room.room_user_num = 2
            print(f"欢迎来到{room.room_id}房间")
        else:
            print("该房间号不存在")

    # 开始游戏
    def start_game(self, user2, user, room):
        if room.room_user_num == 2:
            while 1:
                if user.gold == 0:
                    print(f"{user.gold}金豆不足！")
                    break
                if user2.gold == 0:
                    print(f"{user2.gold}金豆不足！")
                    break
                s = input("输入任意键开始游戏,输入q退出游戏！")
                if s == "q":
                    break

                # 0 表示剪刀
                # 1 表示石头
                # 2 表示布
                # 剪刀大于布 布大于石头 石头大于剪刀
                # 0     2   2     1   1     0
                num1 = random.randint(0, 2)
                num2 = random.randint(0, 2)

                if num1 == 0 and num2 == 2 or num1 == 2 and num2 == 1 or num1 == 1 and num2 == 0:
                    # user1 win
                    print(user.name, "赢了")
                    user2.gold += 100
                    user.gold -= 100
                    print(f"{user.name}还有{user2.gold}元")
                else:
                    print(user.name, "赢了")
                    user2.gold -= 100
                    user.gold += 100
                    print(f"{user.name}还有{user2.gold}元")


def main():
    print("欢迎来到猜拳游戏".center(40, "*"))
    # 加载用户信息
    user_dict = load_user()
    print("我们现在有的用户信息是:", user_dict)

    while 1:
        c = input("1.登录用户\n2.注册用户\n3.按q退出游戏\n请输入你的选择：")
        if c == "q":
            break
        if c == "1":
            print("欢迎来到登录界面".center(40, "*"))
            name = input("请输入你的用户名：")
            # 用户登录
            # 先实例化用户
            user = User(name)
            # 调用登录方法
            user.login(user_dict, name)

            # 加载房间信息
            room_dict = load_room()
            print("我们现在有的房间是：", room_dict)

            while 1:
                room_id_choice = input("1.创建房间\n2，进入房间\n3.按q退出\n请输入你的选择：")
                if room_id_choice == "1":
                    print("欢迎来到创建房间界面".center(40, "*"))
                    room_id = input("请输入你想创建的房间名：")
                    if room_id in room_dict:
                        print("该房间号已经被创建")
                    else:
                        room = Room(room_id)
                        room_dict[room_id] = {"num": room.room_user_num}
                        up_room(room_dict)
                        print("创建房间成功")
                if room_id_choice == "2":
                    print("欢迎来到进入房间界面".center(40, "*"))
                    print("我们现在的房间号有：", room_dict)
                    room_id = input("请输入你要进入的房间号")
                    room = Room(room_id)
                    # 实例化一个用户陪user玩
                    user2 = User("sys")
                    room.get_into_room(room, room_dict)
                    room.start_game( user2, user, room)

        if c == "2":
            print("欢迎来到注册界面".center(40, "*"))
            name = input("请输入你的用户名：")
            if name in user_dict:
                print("该用户名已经被注册")
            else:
                user = User(name)
                user_dict[name] = {"gold": user.gold}
                up_user(user_dict)
                print("恭喜你注册用户成功")
                # print("我们现在的用户信息有：", user_dict)


main()
